import requests
import json

with open("./ignore/config.json","r") as f:
    config=json.load(f)

msg_id = "af1260b4-d628-4f63-aa5f-5ddfcaae6f0e"

def send_message(content, channel_id):
    bot_token=config["bot_token"]

    base_url="https://www.kookapp.cn/api/v3/message/create"
    headers = {
        "Authorization": f"Bot {bot_token}",
        "Content-Type": "application/json"
    }

    # 请求体参数
    payload = {
        "target_id": channel_id,  # 必须字段
        "content": content
    }

    try:
        response = requests.post(
            base_url,
            headers=headers,
            data=json.dumps(payload)  # 将字典转为JSON字符串
        )
        # print(response.json())  # 打印API返回结果
        
        if response.status_code == 200:
            # print("请求成功！")
            res = response.json()
            print(res)
            return res["data"]["msg_id"]
        else:
            print(f"请求失败，状态码: {response.status_code}")
            print("错误信息:", response.text)

    except requests.exceptions.RequestException as e:
        print("请求发生错误:", e)


def update_message(content, msg_id):
    bot_token=config["bot_token"]

    base_url="https://www.kookapp.cn/api/v3/message/update"
    headers = {
        "Authorization": f"Bot {bot_token}",
        "Content-Type": "application/json"
    }

    # 请求体参数
    payload = {
        "msg_id": msg_id,  # 必须字段
        "content": content
    }

    try:
        response = requests.post(
            base_url,
            headers=headers,
            data=json.dumps(payload)  # 将字典转为JSON字符串
        )
        print(response.json())  # 打印API返回结果
        
        if response.status_code == 200:
            # print("请求成功！")
            res = response.json()
            print(res)
        else:
            print(f"请求失败，状态码: {response.status_code}")
            print("错误信息:", response.text)

    except requests.exceptions.RequestException as e:
        print("请求发生错误:", e)


if __name__ == "__main__":
    send_message("hello update", config["channel1_id"])